home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / util / boot / HDEnv12.lha / HDEnv / HDEnv.c < prev    next >
C/C++ Source or Header  |  1995-09-01  |  9KB  |  307 lines

  1. /****************************************************************************\
  2.  
  3. HDEnv 1.2 (1.9.95)
  4. Copyright (C) 1995 by Michael Fedrowitz <mfedrowi@ix.urz.uni-heidelberg.de>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. \****************************************************************************/
  21.  
  22.  
  23. #include <exec/types.h>
  24. #include <exec/memory.h>
  25. #include <dos/dos.h>
  26. #include <proto/dos.h>
  27. #include <proto/exec.h>
  28. #include <stdlib.h>
  29. #include <strings.h>
  30. #include <dos.h>
  31.  
  32.  
  33. #define VERSIONTAG "$VER: HDEnv 1.2 " __AMIGADATE__
  34.  
  35. #define ERR_BREAK 10001
  36.  
  37. #define COPYBUF 2048
  38.  
  39.  
  40. struct HDEnv {
  41.  
  42.     char *path;
  43.     struct FileInfoBlock *fib;
  44.     void *copybuf;
  45.     long test,verbose;
  46. };
  47.  
  48.  
  49. const char *versiontag = VERSIONTAG;
  50.  
  51. extern long __oslibversion = 37;
  52.  
  53. struct HDEnv *hde;
  54.  
  55. long error_code = 0;
  56.  
  57.  
  58. void __regargs _CXBRK(void) {
  59.  
  60.     error_code = ERR_BREAK;
  61. }
  62.  
  63.  
  64. void free_all(struct HDEnv *hde) {
  65.  
  66.     if(hde->path) FreeMem(hde->path,256);
  67.     if(hde->fib) FreeDosObject(DOS_FIB,hde->fib);
  68.     if(hde->copybuf) FreeMem(hde->copybuf,COPYBUF);
  69.  
  70.     FreeMem(hde,sizeof(struct HDEnv));
  71. }
  72.  
  73. struct HDEnv *alloc_all(void) {
  74.  
  75.     struct HDEnv *hde;
  76.  
  77.     if(hde = AllocMem(sizeof(struct HDEnv),MEMF_CLEAR)) {
  78.         if(!(hde->path = AllocMem(256,MEMF_CLEAR))) error_code = ERROR_NO_FREE_STORE;
  79.         if(!(hde->fib = AllocDosObject(DOS_FIB,NULL))) error_code = ERROR_NO_FREE_STORE;
  80.         if(!(hde->copybuf = AllocMem(COPYBUF,MEMF_CLEAR))) error_code = ERROR_NO_FREE_STORE;
  81.     }
  82.     else error_code = ERROR_NO_FREE_STORE;
  83.  
  84.     return hde;
  85. }
  86.  
  87.  
  88. void del_file(char *name) {
  89.  
  90.     BPTR l;
  91.  
  92.     strmfp(hde->path,"ENVARC:",name);
  93.     if(!(l = Lock(hde->path,ACCESS_READ))) {
  94.         if(hde->test) Printf("ENV:%s\n",name);
  95.         else {
  96.             if(hde->verbose) Printf("deleting ENV:%s...\n",name);
  97.             if(!(DeleteFile(name))) error_code = IoErr();
  98.         }
  99.     }
  100.     else UnLock(l);
  101. }
  102.  
  103. void del_dir(char *dir) {
  104.  
  105.     BOOL rc;
  106.     BPTR l;
  107.     long err;
  108.     char *path;
  109.     struct FileInfoBlock *fib;
  110.  
  111.     if(fib = AllocDosObject(DOS_FIB,NULL)) {
  112.         if(path = AllocMem(256,MEMF_CLEAR)) {
  113.             if(l = Lock(dir,ACCESS_READ)) {
  114.                 rc = Examine(l,fib);
  115.                 if(rc) rc = ExNext(l,fib);
  116.                 while(rc) {
  117.                     strmfp(path,dir,fib->fib_FileName);
  118.                     if(fib->fib_DirEntryType > 0) del_dir(path);
  119.                     else del_file(path);
  120.                     chkabort();
  121.                     if(error_code) break;
  122.                     rc = ExNext(l,fib);
  123.                 }
  124.                 if(!error_code) {
  125.                     err = IoErr();
  126.                     if(err != ERROR_NO_MORE_ENTRIES) error_code = err;
  127.                 }
  128.                 UnLock(l);
  129.             }
  130.             if(!error_code) {
  131.                 strmfp(path,"ENVARC:",dir);
  132.                 if(!(l = Lock(path,ACCESS_READ))) {
  133.                     if(hde->test) Printf("ENV:%s (dir)\n",dir);
  134.                     else {
  135.                         if(hde->verbose) Printf("deleting ENV:%s...\n",dir);
  136.                         if(!(DeleteFile(dir))) error_code = IoErr();
  137.                     }
  138.                 }
  139.                 else UnLock(l);
  140.             }
  141.             FreeMem(path,256);
  142.         }
  143.         else error_code = ERROR_NO_FREE_STORE;
  144.         FreeDosObject(DOS_FIB,fib);
  145.     }
  146.     else error_code = ERROR_NO_FREE_STORE;
  147. }
  148.  
  149.  
  150. void copy_file(char *name,struct FileInfoBlock *src_fib) {
  151.  
  152.     BOOL copy = FALSE;
  153.     BPTR l,src_fh,dest_fh;
  154.     long size,bufsize,allocsize;
  155.     void *buf=NULL;
  156.  
  157.     strmfp(hde->path,"ENV:",name);
  158.     if(!(l = Lock(hde->path,ACCESS_READ))) copy = TRUE;
  159.     else {
  160.         Examine(l,hde->fib);
  161.         if(CompareDates(&src_fib->fib_Date,&hde->fib->fib_Date) != 0)
  162.             copy = TRUE;
  163.         UnLock(l);
  164.     }
  165.  
  166.     if(copy) {
  167.         if(hde->test) Printf("ENVARC:%s to %s\n",name,hde->path);
  168.         else {
  169.             if(hde->verbose) Printf("copying ENVARC:%s to %s...\n",name,hde->path);
  170.             if(src_fh = Open(name,MODE_OLDFILE)) {
  171.                 if(dest_fh = Open(hde->path,MODE_NEWFILE)) {
  172.                     size = src_fib->fib_Size;
  173.                     if(size > COPYBUF) {
  174.                         if(buf = AllocMem(size,MEMF_CLEAR)) {
  175.                             allocsize = size;
  176.                             bufsize = size;
  177.                         }
  178.                     }
  179.                     if(!buf) {
  180.                         buf = hde->copybuf;
  181.                         bufsize = COPYBUF;
  182.                         allocsize = 0;
  183.                     }
  184.                     while(size > 0) {
  185.                         if(size < bufsize) bufsize = size;
  186.                         if(Read(src_fh,buf,bufsize) == bufsize)
  187.                             Write(dest_fh,buf,bufsize);
  188.                         if(error_code = IoErr()) break;
  189.                         if(size < bufsize) bufsize = size;
  190.                         size -= bufsize;
  191.                     }
  192.                     if(allocsize) FreeMem(buf,allocsize);
  193.                     Close(dest_fh);
  194.                     if(error_code) DeleteFile(hde->path);
  195.                     else {
  196.                         SetFileDate(hde->path,&src_fib->fib_Date);
  197.                         SetProtection(hde->path,src_fib->fib_Protection);
  198.                         SetComment(hde->path,src_fib->fib_Comment);
  199.                     }
  200.                 }
  201.                 else error_code = IoErr();
  202.                 Close(src_fh);
  203.             }
  204.             else error_code = IoErr();
  205.         }
  206.     }
  207. }
  208.  
  209. void copy_dir(char *dir) {
  210.  
  211.     BOOL rc;
  212.     BPTR l;
  213.     long err;
  214.     char *path;
  215.     struct FileInfoBlock *fib;
  216.  
  217.     if(fib = AllocDosObject(DOS_FIB,NULL)) {
  218.         if(path = AllocMem(256,MEMF_CLEAR)) {
  219.             strmfp(path,"ENV:",dir);
  220.             if(!(l = Lock(path,ACCESS_READ))) {
  221.                 if(hde->test) Printf("%s (created)\n",path);
  222.                 else {
  223.                     if(hde->verbose) Printf("creating %s...\n",path);
  224.                     l = CreateDir(path);
  225.                 }
  226.             }
  227.  
  228.             if(l || hde->test) {
  229.                 if(l) UnLock(l);
  230.                 if(l = Lock(dir,ACCESS_READ)) {
  231.                     rc = Examine(l,fib);
  232.                     if(rc) rc = ExNext(l,fib);
  233.                     while(rc) {
  234.                         strmfp(path,dir,fib->fib_FileName);
  235.                         if(fib->fib_DirEntryType > 0) copy_dir(path);
  236.                         else copy_file(path,fib);
  237.                         chkabort();
  238.                         if(error_code) break;
  239.                         rc = ExNext(l,fib);
  240.                     }
  241.                     if(!error_code) {
  242.                         err = IoErr();
  243.                         if(err != ERROR_NO_MORE_ENTRIES) error_code = err;
  244.                     }
  245.                     UnLock(l);
  246.                 }
  247.             }
  248.             else error_code = IoErr();
  249.             FreeMem(path,256);
  250.         }
  251.         else error_code = ERROR_NO_FREE_STORE;
  252.         FreeDosObject(DOS_FIB,fib);
  253.     }
  254.     else error_code = ERROR_NO_FREE_STORE;
  255. }
  256.  
  257.  
  258. int main(void) {
  259.  
  260.     struct RDArgs *rda;
  261.     long arg[2] = { 0,0 };
  262.     BPTR l,old = NULL;
  263.  
  264.  
  265.     hde = alloc_all();
  266.  
  267.     if(!error_code) {
  268.         if(rda = ReadArgs("TEST/S,VERBOSE/S",arg,NULL)) {
  269.             hde->test = arg[0];
  270.             hde->verbose = arg[1];
  271.             FreeArgs(rda);
  272.             if(l = Lock("ENV:",ACCESS_READ)) {
  273.                 old = CurrentDir(l);
  274.                 if(hde->test) Printf("Would have deleted:\n");
  275.                 del_dir("");
  276.                 UnLock(l);
  277.             }
  278.             else error_code = IoErr();
  279.         }
  280.         else error_code = IoErr();
  281.  
  282.         if(!error_code) {
  283.             if(l = Lock("ENVARC:",ACCESS_READ)) {
  284.                 CurrentDir(l);
  285.                 if(hde->test) Printf("Would have copied:\n");
  286.                 copy_dir("");
  287.                 UnLock(l);
  288.             }
  289.             else error_code = IoErr();
  290.         }
  291.  
  292.         if(old) CurrentDir(old);
  293.     }
  294.  
  295.     if(hde) free_all(hde);
  296.  
  297.     if(error_code) {
  298.         if(error_code != ERR_BREAK) {
  299.             PrintFault(error_code,"HDEnv");
  300.             return RETURN_WARN;
  301.         }
  302.         else PutStr("*** Break\n");
  303.     }
  304.  
  305.     return RETURN_OK;
  306. }
  307.